home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cserial.zip / QUEUE.C < prev    next >
C/C++ Source or Header  |  1990-04-04  |  1KB  |  71 lines

  1. /*
  2.  *                               QUEUE.C
  3.  *
  4.  *                        Queue Handling Module
  5.  *
  6.  *                           Written for the
  7.  *
  8.  *                              Datalight
  9.  *                           Microsoft V 5.x
  10.  *                                TurboC
  11.  *                                  &
  12.  *                               Zortech
  13.  *
  14.  *                             C Compilers
  15.  *
  16.  *            Copyright (c) John Birchfield 1987, 1988, 1989
  17.  */
  18.  
  19. #include "queue.h"
  20.  
  21. /*
  22.  *    QUEUE routines to allocate - enqueue - dequeue elements to a queue
  23.  */
  24.  
  25. QUEUE  *
  26. alloc_queue (int size)
  27. {
  28.     QUEUE  *tmp;
  29.     if ((tmp = (QUEUE *) malloc (sizeof (QUEUE) + size)) != (QUEUE *) 0)
  30.     {
  31.         tmp->size = size;
  32.         tmp->head = 0;
  33.         tmp->tail = 0;
  34.         tmp->avail = size;
  35.         tmp->buf = ((char *) tmp) + sizeof (QUEUE);
  36.     }
  37.     return (tmp);
  38. }
  39.  
  40.  
  41.  
  42. int 
  43. en_queue (QUEUE *qp, char ch)
  44. {
  45.     int     head = qp->head;
  46.  
  47.     if (qp->avail == 0)
  48.         return (-1);
  49.     *(qp->buf + head) = ch;
  50.     if (++head == qp->size)
  51.         head = 0;
  52.     qp->head = head;
  53.     --(qp->avail);
  54.     return (qp->avail);
  55. }
  56.  
  57. int 
  58. de_queue (QUEUE *qp)
  59. {
  60.     int     tail = qp->tail, ch;
  61.  
  62.     if (qp->avail == qp->size)
  63.         return (-1);
  64.     ch = *(qp->buf + tail);
  65.     if (++tail == qp->size)
  66.         tail = 0;
  67.     qp->tail = tail;
  68.     qp->avail++;
  69.     return (ch);
  70. }
  71.